﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Sio.Mdm.BF.Idm;
using Sio.Mdm.Types.Database;
using Ext.Net;
using Sio.Mdm.Entities;
using Sio.Mdm.Client;
using Sio.Mdm.BF.Sifranti;
using Sio.Mdm.Types;
using System.Collections.Generic;

namespace Sio.Mdm.Web.Uporabnik
{
  public partial class UceciPodrobnosti : Controls.Page
  {    
    protected void Page_Load(object sender, EventArgs e)
    {
      if (!IsPostBack && !X.IsAjaxRequest)
      {
          
        //prebermo index Vloga_ID
        NaloziPodatke(getInputInteger("vlogaid"));  
      }
    }
    public void NaloziPodatke(int vlogaId)
    {
      if (vlogaId < 0)
      {
        vlogaId = 100;
      }
      //nastvimo privzete vrednosti
      int stars1 = 0;
      int stars2 = 0;
      
      //občine
			EntityCollection<Obcina> obcine = new EntityCollection<Obcina>(
				new Filter<Obcina>().Where(o => o.ObcinaId != null).OrderBy(o => o.Naziv));
			this.StoreObcine.DataSource = obcine;
      this.StoreObcine.DataBind();

      //pošte
			EntityCollection<PostnaStevilka> poste = new EntityCollection<PostnaStevilka>(
				new Filter<PostnaStevilka>().Where(o => o.PostaId != null).OrderBy(o => o.PostnaStevilka));
			this.StorePoste.DataSource = poste;
      this.StorePoste.DataBind();

      //učeči osebni podatki
			EntityCollection<Ucec> uceci = new EntityCollection<Ucec>(
				new Filter<Ucec>().Where(r => r.VlogaId == vlogaId));

      if (uceci.Count > 0)
      {
        foreach (var xUcec in uceci)
        {          
          //this.tfIDM.value = xUcec.IdmStevilka;
          this.tfNaslov.Value = xUcec.Naslov;
					this.cbPoste.Value = xUcec.PostaId.ToString() == "-1" ? 0 : Convert.ToInt32(xUcec.PostaId);
					this.cbObcine.Value = xUcec.ObcinaId.ToString() == "-1" ? 0 : Convert.ToInt32(xUcec.ObcinaId);
					this.tfEnaslov.Value = xUcec.Email;//xUcec.Ime+"."+xUcec.Priimek+"@sio.si";
          //this.tfTelefon.Value = xUcec.telefon;
          this.tfDrzavljanstvo.Value = xUcec.Drzavljanstvo == null ? "" : xUcec.Drzavljanstvo;
          this.nfEMSO.Text = Convert.ToString(xUcec.Emso) == "-1" ? "" : Convert.ToString(xUcec.Emso);
          this.nfDavcna.Text = Convert.ToString(xUcec.DavcnaStevilka) == "-1" ? "" : Convert.ToString(xUcec.DavcnaStevilka);
 
          stars1 = xUcec.Stars1;
          stars2 = xUcec.Stars2;
        }
      }

      //učeči statusi
      //var slServiceUcecSatatus = ServiceFactory.GetService<IUceciStatusiService>();
      //EntityCollection<UcecStatus> uceciStatusi = null;

      VlogaStatusGrid.LoadData<UcecStatus>(
                    from r in new Filter<UcecStatus>()                    
                    where
                        r.VlogaId == vlogaId
                    select r
                );
      //var errorUcecStatus = slServiceUcecSatatus.Select(ref uceciStatusi, r => r.VlogaId == vlogaId, this.UserSession);

      //StoreStatusiSeznam.DataSource = uceciStatusi;
      //StoreStatusiSeznam.DataBind();

      //starš1
			EntityCollection<Stars> Estars1 = new EntityCollection<Stars>(
									new Filter<Stars>().Where(r => r.VlogaId == stars1));

      var starsime = "";
      if (Estars1.Count > 0)
      {
        int idx = 0;
        foreach (var xStars1 in Estars1)
        {
          this.tfTelefon1.Value = xStars1.Telefon;
          this.tfImePriimek1.Value = xStars1.Ime + ' ' + xStars1.Priimek;
          starsime = xStars1.Ime + ' ' + xStars1.Priimek;
          ++idx;
        }
      }
      this.btnStars1.Listeners.Click.Handler = "parent.addTab(parent.parent.TabNavigation, 'idStars1', '../Uporabnik/StarsPodrobnosti.aspx?stars=" + stars1 + "','Starš " + starsime + "');";

      //starš2
			EntityCollection<Stars> Estars2 = new EntityCollection<Stars>(
						new Filter<Stars>().Where(r => r.VlogaId == stars2));

      if (Estars2.Count > 0)
      {
        foreach (var xStars2 in Estars2)
        {
          this.tfTelefon2.Value = xStars2.Telefon;
          this.tfImePriimek2.Value = xStars2.Ime + ' ' + xStars2.Priimek;
          starsime = xStars2.Ime + ' ' + xStars2.Priimek;
        }
      }
      this.btnStars2.Listeners.Click.Handler = "parent.addTab(parent.parent.TabNavigation, 'idStars2', '../Uporabnik/StarsPodrobnosti.aspx?stars=" + stars2 + "','Starš " + starsime + "');";

      this.btnShrani.Listeners.Click.Handler = "if (#{FormPanelPodrobnosti}.getForm().isValid()) {Ext.net.DirectMethods.ShraniUceciPodrobnosti(" + vlogaId + ");}else{Ext.Msg.show({icon: Ext.MessageBox.ERROR, msg: 'Prosimo vnesite zahtevana polja.', buttons:Ext.Msg.OK});}";
      this.btnPreklici.Listeners.Click.Handler = "Ext.net.DirectMethods.PrekliciUceciPodrobnosti(" + vlogaId + ");";
    }

    [DirectMethod(ShowMask = true)]
    public void ShraniUceciPodrobnosti(int IdmStevilka)
    {
      if (IdmStevilka > 0)
      {
        Ucec ucec = EntityFactory.LoadEntity<Ucec>(
              new Filter<Ucec>().Where(r => r.IdmStevilka == IdmStevilka)
          );
        if (ucec.EntityState == EntityState.Unchanged)
        {
          ucec.Naslov = this.tfNaslov.Value.ToString();
          ucec.Emso = Convert.ToInt64(this.nfEMSO.Value);
          ucec.DavcnaStevilka = Convert.ToInt32(this.nfDavcna.Value);
          ucec.PostaId = Convert.ToInt16(this.cbPoste.Value);
          if (ucec.PostaId < 1)
          {
            ucec.PostaId = null;
          }
          ucec.ObcinaId = Convert.ToInt16(this.cbObcine.SelectedItem.Value);
          if (ucec.ObcinaId < 1)
          {
            ucec.ObcinaId = null;
          }
          ucec.Drzavljanstvo = this.tfDrzavljanstvo.Value.ToString();
          ucec.Email = this.tfEnaslov.Value.ToString();
          //uceci[0].telfon = this.nfTelefon.Value.ToString();
          ucec.Update();
        }
      }
      NaloziPodatke(IdmStevilka);
    }
    [DirectMethod(ShowMask = true)]
    public void PrekliciUceciPodrobnosti(int vlogaId)
    {
      this.tfNaslov.Value = "";
			this.cbPoste.Value = 0;
			this.cbObcine.Value = 0;
      this.tfEnaslov.Value = "";
      //this.nfTelefon.Value = "";
      this.tfDrzavljanstvo.Value = "";
      this.nfEMSO.Value = "";
      this.nfDavcna.Value = "";
      NaloziPodatke(vlogaId);
    }
  }
}
